---
--类名: NpcInteractive.lua
--创建日期: 2017-11-7
--创建人: 陈小虎
--功能描述: 
--修改内容:
--修改日期:   
--修改人:     
--修改备注: 
--

local MoLayer = require("app.widget.MoLayer")

local NpcInteractive = class("NpcInteractive",function()
	return MoLayer.new()
end)

function NpcInteractive:ctor(npcId,returnCall)
	cc(self):addComponent("components.behavior.EventProtocol"):exportMethods()

	self._returnCall = returnCall

	self.mUINode = cc.uiloader:load(Res.NpcInteractiveUi)
    self:addChild(self.mUINode)
    self.mUINode:setContentSize(display.width,display.height)
    ccui.Helper:doLayout(self.mUINode)


    local panelRoot = self.mUINode:getChildByName("Panel")
    panelRoot:setTouchEnabled(true)

    local nameNode = panelRoot:getChildByName("name")
    local desNode = panelRoot:getChildByName("Text")
    local headRoot = panelRoot:getChildByName("npc")
    local bgImage = panelRoot:getChildByName("bg01")
    local btnsRoot = panelRoot:getChildByName("Panel_btn")
    btnsRoot:setTouchEnabled(false)
    btnsRoot:removeAllChildren()

    UIHandler:addTopTouchMask(function(touch)
        local locationInNode = bgImage:convertToNodeSpace(touch:getLocation())
        local s = bgImage:getContentSize()
        if not cc.rectContainsPoint(cc.rect(0, 0, s.width, s.height), locationInNode) then
            self:callback()
            UIHandler:hideInteractiveUi()
            UIHandler:removeTopTouchMask()
        end
    end)

    local npcC = GD:queryMapNpc(npcId)

    local config = GD:queryFunctionNpcById(npcId)
    if not config or not config.ID then return end
    local des = config.introduction and L(config.introduction) or ""
    local name = npcC.name and L(npcC.name) or ""

    nameNode:setString(name)
    desNode:setString(des)

    local funcs = config.functionbutton and string.split(config.functionbutton,"|") or nil
    local images = config.buttonicon and string.split(config.buttonicon,";") or {}
    if funcs then
    	for i,id in ipairs(funcs) do
	    	local btnNode = cc.uiloader:load(Res.NpcInteractiveNode)
	    	local btnRoot = btnNode:getChildByName("Panel")
	    	local button = btnRoot:getChildByName("Button")
	    	local x = btnsRoot:getContentSize().width - 95 - (i - 1) * btnRoot:getContentSize().width
	    	local y = 0
	    	btnNode:setPosition(cc.p(x,y))
	    	btnsRoot:addChild(btnNode)
	    	
	    	local iImages = images[i] and string.split(images[i],"|") or {}
	    	local normalIconPath = GD:getImagePath(Res.functionNpcIconPath,iImages[1])
	    	local pressedIconPath = GD:getImagePath(Res.functionNpcIconPath,iImages[2])
            if normalIconPath then
                button:loadTextureNormal(normalIconPath)
            end
            if pressedIconPath then
                button:loadTexturePressed(pressedIconPath)
            end

            button:addClickEventListener(function(sender)
            	self:callback()
            	UIHandler:hideInteractiveUi()
            	UIHandler:handleMessage({id = tonumber(id), mode = "open"})
            end)
    	end
    end

    local headPath = GD:getImagePath(Res.functionNpcIconPath,config.headportrait)
    if headPath then
        local head = ccui.ImageView:create(headPath)
        head:setAnchorPoint(cc.p(0.5,0))
        head:setPosition(cc.p(headRoot:getContentSize().width / 2,0))
        headRoot:addChild(head)
        headRoot:setVisible(true)
    end
end

function NpcInteractive:callback()
    if self._returnCall then
        self._returnCall()
    end
end


return NpcInteractive